"""
家 | 接口
"""

from fastapi import (
    APIRouter,
    Depends,
)

import utils.enum_utils as enum
from biz.family_biz import home_biz
from components.audit_log import audit
from components.depends import login_required
from components.global_param import get_user_id
from components.logger import log
from components.response import (
    OKResponse,
    OKWithDataResponse,
)
from models import OK
from models.request import (
    FamilyHomeCreateRequest,
    FamilyHomeDetailRequest,
    FamilyHomeListRequest,
    FamilyHomePathField,
    FamilyHomeUpdateRequest,
)
from models.response import (
    FamilyHomeDetailResponse,
    FamilyHomeListResponse,
)
from models.vo import (
    FamilyHomeDetailVO,
    FamilyHomeListPage,
)
from utils.data_utils.special import ObjectIdStr

home_router: APIRouter = APIRouter(prefix="/home")
""" 家 路由 """


@home_router.post(
    path="/",
    summary="家 | 创建",
    response_model=FamilyHomeDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.FAMILY_HOME,
    operation=enum.ModelOperation.UPLOAD,
    po_id="response.data.home_id",
)
async def home_create(request: FamilyHomeCreateRequest) -> OKWithDataResponse:
    vo: FamilyHomeDetailVO = home_biz.FamilyHomeCreator.create(request)
    log.info(f"[家][创建] user_id: {get_user_id()} home_id: {vo.home_id}")

    return OKWithDataResponse(vo)


@home_router.delete(
    path="/{home_id}/",
    summary="家 | 删除",
    response_model=OK,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.FAMILY_HOME,
    operation=enum.ModelOperation.DELETE,
    po_id="home_id",
)
async def home_delete(home_id: ObjectIdStr = FamilyHomePathField.home_id) -> OKResponse:
    request = FamilyHomeDetailRequest(home_id=home_id)
    home_biz.FamilyHomeDeleter.delete(request)
    log.info(f"[家][删除] user_id: {get_user_id()} home_id: {home_id}")

    return OKResponse()


@home_router.put(
    path="/{home_id}/",
    summary="家 | 更新",
    response_model=FamilyHomeDetailResponse,
    dependencies=[Depends(login_required)],
)
@audit(
    model=enum.Model.FAMILY_HOME,
    operation=enum.ModelOperation.UPDATE,
    po_id="home_id",
)
async def home_update(
    request: FamilyHomeUpdateRequest,
    home_id: ObjectIdStr = FamilyHomePathField.home_id,
) -> OKWithDataResponse:
    request.home_id = home_id
    vo: FamilyHomeDetailVO = home_biz.FamilyHomeUpdator.update(request)
    log.info(f"[家][更新] user_id: {get_user_id()} home_id: {home_id}")

    return OKWithDataResponse(vo)


@home_router.get(
    path="/{home_id}/",
    summary="家 | 详情",
    response_model=FamilyHomeDetailResponse,
)
@audit(
    model=enum.Model.FAMILY_HOME,
    operation=enum.ModelOperation.DETAIL,
    po_id="home_id",
)
async def home_detail(home_id: ObjectIdStr = FamilyHomePathField.home_id) -> OKWithDataResponse:
    request = FamilyHomeDetailRequest(home_id=home_id)
    vo: FamilyHomeDetailVO = home_biz.FamilyHomeGetter.detail(request)
    log.info(f"[家][详情] user_id: {get_user_id()} home_id: {home_id}")

    return OKWithDataResponse(vo)


@home_router.get(
    path="/",
    summary="家 | 列表",
    response_model=FamilyHomeListResponse,
)
@audit(
    model=enum.Model.FAMILY_HOME,
    operation=enum.ModelOperation.LIST,
    po_id="response.data.data_list.home_id",
)
async def home_list(request: FamilyHomeListRequest = Depends(FamilyHomeListRequest.from_query)) -> OKWithDataResponse:
    vo: FamilyHomeListPage = home_biz.FamilyHomeGetter.list(request)
    log.info(f"[家][列表] user_id: {get_user_id()}")

    return OKWithDataResponse(vo)
